<template>
	<div class="wrapper">
		<header>购物车案例</header>
		<div class="container center" v-for="item in list" :key="item.id">
			<input type="checkbox" v-model="item.goods_state" />
			<img :src="item.goods_img" />
			<div class="intro x-center">
				<div class="title">{{ item.goods_name }}</div>
				<div class="count-wrapper x-center">
					<div class="price">￥ {{ item.goods_price }}</div>
					<div class="count x-center">
						<div @click="item.goods_count && item.goods_count--">-</div>
						<input type="text" :value="item.goods_count" @input="(e) => checkInp (e, item)" @blur="onBlur"/>
						<div @click="item.goods_count++">+</div>
					</div>
				</div>
			</div>
		</div>
		<footer class="center">
			<div>
				<input type="checkbox" name="" id="all" v-model="isChecked"  @click="selectAll"/>
				<label for="all">全选</label>
			</div>
			<div>
				<span>合计：</span>
				<span class="total-price">￥ {{ getSum }}</span>
			</div>
			<div class="settlement">
				<div class="text">结算({{  this.showingList.reduce((pre, current) => pre + current.goods_count, 0) }})</div>
			</div>
		</footer>
	</div>
</template>

<script>
export default {
	data() {
		return {
			list: [],
			showingList: [],
			counter: 0,
            isChecked: false,
		};
	},
	created () {
		this.onSearch();
	},
    updated () {
        this.isChecked = this.checkNum(this.list);
    },
	computed: {
		getSum () {
			this.showingList = this.list.filter((item) => item.goods_state);
			return this.showingList.reduce((pre, current) => pre + current.goods_count * current.goods_price, 0);},
	},
	methods: {
		async onSearch () {
			const res = await this.http({
				method: 'get',
				url: 'https://www.escook.cn/api/cart',
			});
			this.list = res.data.list;
		},
        checkInp (e, item) {
            if (!isNaN(e.target.value) && e.target.value >= 0) {
                item.goods_count = Number(e.target.value);
            }
        },
        onBlur () {
            this.list = this.list.concat();
        },
        selectAll () {
            for (const item of this.list) {
                item.goods_state = !this.isChecked;
                this.list = this.list.concat();
            }
        },
        checkNum (arr) {
            return arr.every(item => item.goods_state);
        }
	},
};
</script>

<style scoped>
.center {
    display: flex;
	justify-content: space-between;
    align-items: center;
}
.x-center {
    display: flex;
	justify-content: space-between;
}
.wrapper {
	margin: 50px 0;
}
header {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 50px;
	color: #fff;
	text-align: center;
	line-height: 50px;
	background-color: #0e79ff;
}
.wrapper .container {
	width: 100%;
	height: 150px;
	padding: 12px;
	border-bottom: 1px solid #eee;
    box-sizing: border-box;
}
.wrapper .container img {
	height: 100%;
	width: 120px;
	margin-left:4px;
	border-radius: 8px;
	display: block;
}
.wrapper .container input {
	margin: 0;
	display: block;
	text-align: center;
}
.wrapper .container .intro {
	flex: 1;
	height: 100%;
	flex-direction: column;
    padding: 4px;
    box-sizing: border-box;
}
.container .intro .title {
    font-weight: 700;
}
.container .count-wrapper {
    width: 100%;
}
.count-wrapper .price,
footer .total-price {
	font-weight: 900;
	color: red;
}
.count-wrapper .count input {
	width: 50px;
	margin: 0 24px;
    padding: 0;
}
footer {
	position: fixed;
	left: 0;
	bottom: 0;
	width: 100%;
	height: 50px;
	background-color: #eee;
	padding: 0 12px;
	box-sizing: border-box;
}
footer .settlement {
    width: 80px;
    height: 30px;
    border-radius: 15px;
    background-color: #0076ff;
    line-height: 30px;
    color: #fff;
    text-align: center;
}

</style>